package com.gupaoedu.design.prototype.deep;

import java.io.*;
import java.util.Date;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2018/3/9.
 */
public class MonkeyKing extends Monkey implements Cloneable,Serializable {
    private String name;

    private GoldBar goldBar;

    @Override
    protected Object clone() {
        return this.deepClone();
    }

    protected Object deepClone() {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
            oos.flush();
            oos.close();

            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            MonkeyKing mk = (MonkeyKing) ois.readObject();
            mk.setName("孙悟空");
            mk.setBirthDay(new Date());
            return mk;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public MonkeyKing copy(MonkeyKing target) {
        try {
            Thread.sleep(2000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        MonkeyKing mk = new MonkeyKing();
        mk.setName(target.getName());
        mk.setHeight(target.getHeight());
        mk.setWeight(target.getWeight());
        mk.setBirthDay(new Date());

        GoldBar gb = new GoldBar();
        gb.setBig(target.goldBar.getBig());
        gb.setFeatures(target.getGoldBar().getFeatures());

        mk.setGoldBar(gb);
        return mk;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public GoldBar getGoldBar() {
        return goldBar;
    }

    public void setGoldBar(GoldBar goldBar) {
        this.goldBar = goldBar;
    }

    @Override
    public String toString() {
        return "MonkeyKing{" +
                "name='" + name + '\'' +
                ", goldBar=" + goldBar +
                ",height=" + getHeight() +
                ",weight="+ getWeight() +
                ",birthDay="+ getBirthDay() +
                "'}'";
    }
}
